import 'package:flutter/widgets.dart';
import 'package:library_base/entities/event/dispatcher_goods_count.dart';
import 'package:library_base/entities/stock_out_child_data_entity.dart';
import 'package:library_base/entities/warehouse_entity.dart';
import 'package:library_base/entities/weighing_goods_child_entity.dart';
import 'package:library_base/entities/weighing_goods_entity.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/net/apis.dart';
import 'package:library_base/network/http_config.dart';
import 'package:library_base/provider/base_list_controller.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:get/get.dart';

class AsGoodsController extends BaseListController<WeighingGoodsEntity, WeighingGoodsChildEntity> {

  String stationTimeBegin = '';

  void Function()? callback;

  Rxn<StockOutChildDataEntity> stockOutChildDataEntity = Rxn<StockOutChildDataEntity>();

  TextEditingController keyWorkSearchController = TextEditingController();

  Rx<String> keyWord = ''.obs;

  WarehouseEntity? warehouse;

  @override
  Future requestData() {
    isFirstLoading = true;
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse!=null?warehouse!.id:"",
        });
    return super.requestData();
  }

  @override
  List<WeighingGoodsChildEntity> transformObject(WeighingGoodsEntity data, bool isRefresh) {
    if(stockOutChildDataEntity.value!= data.data){
      stockOutChildDataEntity.value = data.data;
    }
    return data.list;
  }

  @override
  void loadAfter() {
    Event.eventBus.fire(DispatcherGoodsCount(1, listData.length));
  }

  @override
  Future onRefreshCallback() async {
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: true,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(true)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse!=null?warehouse!.id:"",
        });
  }

  @override
  Future onLoadCallback() async {
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse!=null?warehouse!.id:"",
        });
  }

  void onSearch(String value) {
    warehouse=null;
    tryReLoad();
  }

  void onChanged(String value){
    if(keyWord.value!= value){
      keyWord.value = value;
      update();
    }
  }

  void backToList() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    keyWorkSearchController.text = "";
    warehouse = null;
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse!=null?warehouse!.id:"",
        });
  }

  void reset() {
    keyWorkSearchController.text = "";
    warehouse = null;
    stationTimeBegin = '';
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse!=null?warehouse!.id:"",
        });
  }

  filter(WarehouseEntity warehouse) {
    this.warehouse = warehouse;
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse.id,
        });
  }

  void tryReLoad() {
    isFirstLoading = true;
    multiControllerOut.viewStateChange(MultiStateStatus.loading);
    baseState?.setStateCallBack?.call();
    requestGeneralListData(Apis.WEIGHING_GOODS_PAGE,
        isRefresh: false,
        method: FBResultFul.GET,
        params: {},
        queryParameters: {
          "pageSize": '$limit',
          "pageNo": '${getPage(false)}',
          "stationTimeBegin": stationTimeBegin,
          "stationTimeEnd": stationTimeBegin,
          "goodsBarCode": keyWorkSearchController.text,
          "warehouseId": warehouse!=null?warehouse!.id:"",
        });
  }

  void onItemClick(WeighingGoodsChildEntity itemData) {
    var map  = {};
    map['stockOutId'] = itemData.stockOutId;
    map['goodsCode'] = itemData.goodsCode;
    map['singleEntry'] = itemData.singleEntry;
    if(itemData.singleEntry){
      map['goodsBarCode'] = itemData.goodsBarCode;
      map['mainWeigh'] = itemData.mainWeigh;
      map['subWeigh'] = itemData.subWeigh;
      map['skinWeight'] = itemData.skinWeight;
    }else{
      map['goodsName'] = itemData.goodsName;
      map['stockOutCount'] = itemData.stockOutCount;
      map['stockOutWeigh'] = itemData.stockOutWeigh;
    }
    map['batchNo'] = itemData.batchNo;
    map['locationId'] = itemData.locationId;
    map['updatePosition'] = _updatePosition;
    DialogUtil.showCreateWmsGoodsDialog(arguments: map);
  }

  void onItemChildClick(WeighingGoodsChildEntity itemData) {

  }

  void platoon() {

  }

  void _updatePosition(){
    tryReLoad();
  }

  void selectedAll() {

  }



}
